package com.zzqstart.cateservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzqstart.cateservice.entity.CateRecipe;
import com.zzqstart.cateservice.entity.CateRecipeCollect;
import com.zzqstart.cateservice.service.CateRecipeCollectService;
import com.zzqstart.cateservice.service.CateRecipeService;
import com.zzqstart.commonutils.JwtUtils;
import com.zzqstart.commonutils.R;
import io.jsonwebtoken.Jwt;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-03-06
 */
@RestController
@RequestMapping("/cateservice/recipecollect")
public class CateRecipeCollectController {
    @Autowired
    private CateRecipeCollectService cateRecipeCollectService;

    @Autowired
    private CateRecipeService cateRecipeService;

    @ApiOperation(value = "查询是否收藏")
    @GetMapping("/isCollectRecipe/{recipeId}")
    public R isCollectRecipe(@PathVariable String recipeId, HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);

        QueryWrapper<CateRecipeCollect> wrapper = new QueryWrapper<>();
        wrapper.eq("recipe_id", recipeId);
        wrapper.eq("member_id", memberId);

        int count = cateRecipeCollectService.count(wrapper);
        return R.ok().data("isCollect", count > 0);
    }

    @ApiOperation(value = "收藏菜谱")
    @PostMapping("/collectRecipe/{recipeId}")
    @Transactional
    public R collectRecipe(@PathVariable String recipeId, HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);

        CateRecipeCollect collect = new CateRecipeCollect();
        collect.setRecipeId(recipeId);
        collect.setMemberId(memberId);
        boolean isSave = cateRecipeCollectService.save(collect);

        QueryWrapper<CateRecipeCollect> wrapper = new QueryWrapper<>();
        wrapper.eq("recipe_id",recipeId);
        int count = cateRecipeCollectService.count(wrapper);
        CateRecipe recipe = new CateRecipe();
        recipe.setId(recipeId);
        recipe.setCollectCount(count);
        cateRecipeService.updateById(recipe);

        if (isSave) {
            return R.ok();
        } else {
            return R.error().message("收藏失败");
        }
    }

    @ApiOperation(value = "取消收藏菜谱")
    @DeleteMapping("/cancelCollectRecipe/{recipeId}")
    public R cancelCollectRecipe(@PathVariable String recipeId, HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);

        QueryWrapper<CateRecipeCollect> wrapper = new QueryWrapper<>();
        wrapper.eq("recipe_id", recipeId);
        wrapper.eq("member_id", memberId);
        boolean isDel = cateRecipeCollectService.remove(wrapper);

        QueryWrapper<CateRecipeCollect> wrapperCollect = new QueryWrapper<>();
        wrapper.eq("recipe_id",recipeId);
        int count = cateRecipeCollectService.count(wrapperCollect);
        CateRecipe recipe = new CateRecipe();
        recipe.setId(recipeId);
        recipe.setCollectCount(count);
        cateRecipeService.updateById(recipe);
        if (isDel) {
            return R.ok();
        } else {
            return R.error().message("收藏失败");
        }
    }

    public List<String> getRecipeIdListBymemberId(HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        List<String> recipeIdList = cateRecipeCollectService.getRecipeIdListBymemberId(memberId);
        return recipeIdList;
    }

}

